﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Swin_e_mon_Forms
{
    public class TextFileWorldLoader:IWorldLoader
    {
        private string[] textFile;

        public TextFileWorldLoader(string textLoc)
        {
            textFile = System.IO.File.ReadAllLines(textLoc);
        }
        
        public Location FindLocation(string id, List<Location> locs)
        {
            foreach(Location l in locs)
            {
                if(l.IdentifiedBy(id)) return l;
            }
            return null;
        }

        public Location Load()
        {
            List<Location> listOfLocs = new List<Location>();
            Inventory inv = new Inventory();
            int line = 2;

            line = LoadItems(inv, line);
            line++;

            line = LoadLocations(listOfLocs, inv, line);
            line++;

            line = LoadPaths(listOfLocs, line);

            line++;
            return FindLocation(textFile[line], listOfLocs);
        }

        private int LoadPaths(List<Location> listOfLocs, int line)
        {
            string title, description;
            string[] ids, locIds;

            int index = line++;

            for (int i = 0; i < int.Parse(textFile[index]); i++)
            {

                ids = textFile[line++].Split(',');
                title = textFile[line++];
                description = textFile[line++];
                locIds = textFile[line++].Split(',');

                Path pth = new Path(ids, FindLocation(locIds[1], listOfLocs));
                pth.Title = title;
                pth.Description = description;

                FindLocation(locIds[0], listOfLocs).paths.Add(pth);
            }
            return line;
        }

        private int LoadLocations(List<Location> listOfLocs, Inventory inv, int line)
        {
            string title, description;
            string[] ids, itemIds;

            int index = line++;

            for (int i = 0; i < int.Parse(textFile[index]); i++)
            {
                ids = textFile[line++].Split(',');
                title = textFile[line++];
                description = textFile[line++];

                Location loc = new Location(ids);
                loc.Title = title;
                loc.Description = description;

                itemIds = textFile[line++].Split(',');
                AddItemsTo(inv, itemIds, loc);

                listOfLocs.Add(loc);
            }
            return line;
        }

        private int LoadItems(Inventory inv, int line)
        {
            string title, description;
            string[] ids, itemIds;
            bool isStationary, isContainer;

            for (int i = 0; i < int.Parse(textFile[1]); i++)
            {
                ids = textFile[line++].Split(',');
                title = textFile[line++];
                description = textFile[line++];
                isStationary = textFile[line++].ToLower() == "stationary";
                isContainer = textFile[line++].ToLower() == "container";

                Item itm = new Item(ids, isContainer);
                itm.Title = title;
                itm.Description = description;
                itm.IsStationary = isStationary;

                if (isContainer)
                {
                    itemIds = textFile[line++].Split(',');
                    AddItemsTo(inv, itemIds, itm);
                }
                inv.AddItem(itm);
            }
            return line;
        }

        private void AddItemsTo(Inventory inv, string[] itemIds, GameEntity itm)
        {
            foreach (string id in itemIds)
            {
                Item toAdd = inv.TakeItem(id);
                if (toAdd == null) throw new Exception("Error in file, unable to find " + id);
                itm.Inventory.AddItem(toAdd);
            }
        }
    }
}
